package com.qianfeng.gl4study.youshare.acitivity.common;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.acitivity.store.StoreActivity;
import com.qianfeng.gl4study.youshare.tasks.PhoneCodeTask;
import com.qianfeng.gl4study.youshare.tasks.RegisterTask;
import com.qianfeng.gl4study.youshare.tasks.TaskProcessor;
import com.qianfeng.gl4study.youshare.utils.Constants;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/24
 * Email:pdsfgl@live.com
 */
public class RegisterActivity extends Activity implements TaskProcessor {

	private EditText phoneNumber;
	private EditText phoneCode;
	private EditText password;
	private EditText conformPassword;
	private String registerGetPhoneCodeUrl = Constants.API_POINT+"/sms.php";
	private String registerUrl = Constants.API_POINT+"/user.php?ac=register";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		displayActivityBar();

		phoneNumber = (EditText) findViewById(R.id.register_user_phone);
		phoneCode = (EditText) findViewById(R.id.register_mobile_code);
		password = (EditText) findViewById(R.id.register_user_password);
		conformPassword = (EditText) findViewById(R.id.register_user_conform_password);
	}

	/**
	 * 点击获取手机验证码
	 * @param view
	 */
	public void getMobileCode(View view){
		String phoneNumberString = phoneNumber.getText().toString();
		if(null==phoneNumberString||phoneNumberString.equals("")){
			Toast.makeText(this,"手机号码不能为空",Toast.LENGTH_LONG).show();
		}else {

			registerGetPhoneCodeUrl +="?mobile="+phoneNumberString;

			//联网进行获取手机验证码操作

			PhoneCodeTask phoneCodeTask = new PhoneCodeTask(this);
			phoneCodeTask.execute(registerGetPhoneCodeUrl);

		}
	}

	/**
	 * 点击注册按钮
	 * @param view
	 */
	public void register(View view){

		String userNameString = phoneNumber.getText().toString().trim();
		String mobileCode = phoneCode.getText().toString().trim();
		String passwordString = password.getText().toString().trim();
		String conformPasswordString = conformPassword.getText().toString().trim();
		if(null==userNameString||userNameString.equals("")){
			//提示用户名不能为空
			Toast.makeText(this,"用户名不能为空",Toast.LENGTH_LONG).show();
		}
		/*
		else if(null==mobileCode||mobileCode.equals("")){
			//提示密码不能为空
			Toast.makeText(this,"验证码不能为空",Toast.LENGTH_LONG).show();
		}*/
		else if(null==passwordString||passwordString.equals("")){
			//提示密码不能为空
			Toast.makeText(this,"密码不能为空",Toast.LENGTH_LONG).show();
		}else if(null==conformPasswordString||conformPasswordString.equals("")){
			//提示密码不能为空
			Toast.makeText(this,"重复密码不能为空",Toast.LENGTH_LONG).show();
		}else {
			RegisterTask registerTask = new RegisterTask(this);
			registerTask.execute(userNameString,passwordString,conformPasswordString,mobileCode);
		}
	}

	@Override
	public void processResult(JSONObject result, int flag) {
		if(flag==1){
			//获取手机验证码
			String resultString = "获取验证码失败";
			try {
				int code = result.getInt("code");
				//code为1，代表返回数据正确，msg是一个对象，若code不是1，代表数据请求失败，msg返回字符串
				if(code == 1) {
					String msg = result.getString("msg");
					resultString = "已发送验证码，请等待";
				}else if(code == 0){
					resultString += "，原因："+result.getString("msg");
				}
				Toast.makeText(this,resultString,Toast.LENGTH_LONG).show();
			}catch (JSONException e) {
				e.printStackTrace();
			}

		}else if(flag == 2){
			String resultString = "注册失败";
			try {
				int code = result.getInt("code");
				//code为1，代表返回数据正确，msg是一个对象，若code不是1，代表数据请求失败，msg返回字符串
				if(code == 1) {
					String userId = result.getJSONObject("msg").getString("userid");
					Constants.USER_ID = userId;
					resultString = "注册成功";
					Intent intent = new Intent(this,StoreActivity.class);
					startActivity(intent);
					finish();
				}else if(code == 0){
					resultString += "，原因:"+result.getString("msg");
				}
				Toast.makeText(this,resultString,Toast.LENGTH_LONG).show();
			}catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	private void displayActivityBar() {
		//去掉ActionBar的图标，标题居中
		ActionBar actionBar = getActionBar();
		if (actionBar != null) {
			actionBar.setDisplayHomeAsUpEnabled(true);
			actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
			actionBar.setDisplayShowCustomEnabled(true);
			LayoutInflater inflater = LayoutInflater.from(this);
			View view = inflater.inflate(R.layout.actionbar_main_title, null);
			// 获取view中的 TextView，进行标题的设置
			TextView txtTitle = (TextView) view.findViewById(R.id.actionbar_title);
			if (txtTitle != null) {
				txtTitle.setText("注册");
				txtTitle.setTextColor(Color.WHITE);
			}
			actionBar.setCustomView(
					view,
					new ActionBar.LayoutParams(
							ViewGroup.LayoutParams.MATCH_PARENT,
							ViewGroup.LayoutParams.WRAP_CONTENT,
							Gravity.CENTER
					)
			);
		}
	}
}